/**
 * created by 乐哥哥, 2019/7/23
 * Copyright (c) 2019, 270628297@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.wyl.main.alarm

import android.app.TimePickerDialog
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.wyl.easydialog.EasyDialogUtil
import com.wyl.libbase.base.BindingFragment
import com.wyl.libbase.binding.recyclerview.RecyclerViewDivider
import com.wyl.libbase.utils.KLog
import com.wyl.main.R
import com.wyl.main.bean.AlarmData
import com.wyl.main.databinding.MainAlarmFragmentBinding
import io.ditclear.bindingadapter.ItemClickPresenter
import io.ditclear.bindingadapter.SingleTypeAdapter
import org.koin.android.viewmodel.ext.android.viewModel
import java.util.*

/**
 * 项目名称：SimpleClock
 * 创建人：乐哥哥
 * 创建时间：2019-07-23 17:47
 */
class AlarmFragment : BindingFragment<MainAlarmFragmentBinding>(), ItemClickPresenter<AlarmData> {

    private val alarmModel: AlarmViewModel by viewModel()

    private val mLayoutManager by lazy { LinearLayoutManager(context!!) }

    private val mAdapter by lazy {
        SingleTypeAdapter(
            context!!,
            R.layout.main_alarm_item,
            alarmModel.dataSource
        ).apply {
            itemPresenter = this@AlarmFragment
        }
    }

    override fun getLayoutId() = R.layout.main_alarm_fragment

    override fun loadData() {
        
    }

    override fun initView() {
        binding.vm = alarmModel

        binding.listLayout.recyclerView.apply {
            layoutManager = mLayoutManager
            adapter = mAdapter
            addItemDecoration(RecyclerViewDivider(context, RecyclerViewDivider.VERTICAL))
        }


    }

    override fun onItemClick(v: View, item: AlarmData) {
        EasyDialogUtil.showConfirmDialog(
            "您是否要删除该闹钟",
            childFragmentManager,
            object : EasyDialogUtil.ConfirmDialogCallback {
                override fun cancel(view: View) {
                    Toast.makeText(context, getString(R.string.common_cancel), Toast.LENGTH_LONG).show()
                }

                override fun confirm(view: View) {
                    Toast.makeText(context, getString(R.string.common_confirm), Toast.LENGTH_LONG).show()
                }
            })
    }


    override fun onClick(v: View) {
        when (v.id) {
            R.id.addAlarm -> {
                KLog.d("addAlarm")
                val calendar = Calendar.getInstance()
                TimePickerDialog(
                    context!!,
                    TimePickerDialog.OnTimeSetListener { _, hourOfDay, minute ->
                        alarmModel.addAlarmData(hourOfDay, minute)
                    },
                    calendar.get(Calendar.HOUR_OF_DAY),
                    calendar.get(Calendar.MINUTE),
                    true
                ).show()
            }
            else -> {

            }
        }
    }

}